﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity;
using System.Configuration;

namespace IISDeploy.Configuration
{
    public class DependencyContainer
    {

        #region Fields

        private IUnityContainer container;        
        private static DependencyContainer instance;

        #endregion


        #region Properties
        /// <summary>
        /// Intância única para acesso a Injeção de Dependência.
        /// </summary>
        public static DependencyContainer Instance
        {
            get
            {
                if (instance == null)
                    instance = new DependencyContainer();
                return instance;
            }
        }

        #endregion

        #region Methods

        private DependencyContainer()
        {
            LoadDependencies();
        }        

        private void LoadDependencies()
        {
            UnityConfigurationSection unityConfigurationSection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            container = new UnityContainer();
            unityConfigurationSection.Containers.Default.Configure(container);
        }

        public T Resolve<T>()
        {
            return this.container.Resolve<T>();
        }
                
        public IUnityContainer Container
        {
            get { return container; }
        } 

        #endregion

      
    }
}
